﻿using System;
using System.Threading;
using System.Web.UI.WebControls;
using Cms.Data;
using Cms.Domain.RepositoryInterfaces;
using Cms.Utilities;
using Cms.Web.Framework.Plugins.Attributes;
using iLib.Web.UI;
using Content = Cms.Domain.Content;

namespace Cms.Web.Framework.Plugins
{
    public class ReadMore : BaseTemplatedPlugin
    {
        // Property fields
        private int _contentID;
        private string _publishedDateFormat;

        // Template controls
        private Panel pnlContent;
        private Literal litName;
        private Literal litTitle;
        private Literal litPublishedBy;
        private Literal litPublishedDate;

        [Browse("content")]
        public int ContentID
        {
            get { return _contentID; }
            set { _contentID = value; }
        }

        public string PublishedDateFormat
        {
            get { return _publishedDateFormat; }
            set { _publishedDateFormat = value; }
        }

        protected override void CreateChildControls()
        {
            try
            {
                base.CreateChildControls();
                pnlContent = (Panel)FindControl("pnlContent");
                litName = (Literal)FindControl("litName");
                litTitle = (Literal)FindControl("litTitle");
                litPublishedBy = (Literal)FindControl("litPublishedBy");
                litPublishedDate = (Literal)FindControl("litPublishedDate");
                IContentRepository contentRepository = new ContentRepository();
                if (_contentID <= 0)
                {
                    _contentID = QueryString.GetInt32ID("contentid");
                }
                Content content = contentRepository.GetContent(_contentID);
                RenderContent(content);
            }
            catch (Exception ex)
            {
                Logging.LogError("ReadMore", ex);
                Controls.Clear();
            }
        }

        private void RenderContent(Content content)
        {
            if (litName != null)
            {
                litName.Text = content.Name;
            }
            if (pnlContent != null)
            {
                ContentParser.Parse(content.GetPublishedHtml(RequestDetails.Locale.ID), pnlContent, RequestDetails.RequestedUrl);
            }
            if (litTitle != null)
            {
                litTitle.Text = content.GetPublishedTitle(RequestDetails.Locale.ID);
            }
            if (litPublishedBy != null)
            {
                litPublishedBy.Text = content.CreatedBy.Fullname;
            }
            if (litPublishedDate != null)
            {
                DateTime publishedDate = content.FirstPublished.HasValue
                                             ? content.FirstPublished.Value
                                             : content.Created;
                litPublishedDate.Text = !string.IsNullOrEmpty(_publishedDateFormat)
                                            ? publishedDate.ToString(_publishedDateFormat, Thread.CurrentThread.CurrentCulture.DateTimeFormat)
                                            : publishedDate.ToString(Thread.CurrentThread.CurrentCulture.DateTimeFormat);
            }
        }
    }
}
